package com.wenhuitec.android.tang.ui.adapter;

import android.content.Context;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.TextView;

import androidx.annotation.NonNull;
import androidx.recyclerview.widget.RecyclerView;

import com.dhkj.web.bean.ProductModel;
import com.dhkj.web.bean.Zweekmenubak;
import com.wenhuitec.android.tang.R;
import com.wenhuitec.android.tang.sysui.model.CarFoodsModel;
import com.wenhuitec.android.tang.sysui.view.AddAndDecreaseButton;

import java.math.BigDecimal;
import java.util.ArrayList;
import java.util.List;

/**
 * 点餐界面左侧的商品列表
 */
public class CarFoodsAdapter extends RecyclerView.Adapter<CarFoodsAdapter.ViewHolder> {

    private List<CarFoodsModel> list = new ArrayList<>();

    private Context mContext;

    private CarFoodsAmountListener carFoodsAmount;

    public void setCarFoodsAmountListener(CarFoodsAmountListener carFoodsAmount) {
        this.carFoodsAmount = carFoodsAmount;
    }

    public CarFoodsAdapter(Context context) {
        this.mContext = context;
    }

    public void addFoods(Zweekmenubak zweekmenubak) {
        CarFoodsModel carFoodsModel = list.stream().filter(u -> u.getBm().equals(zweekmenubak.getBm()))
                .filter(u -> u.getAddr().intValue() == zweekmenubak.getAddr().intValue()).findFirst().orElse(null);
        if (carFoodsModel != null) {
            carFoodsModel.setFoodNumber(carFoodsModel.getFoodNumber() + 1);
        } else {
            carFoodsModel = new CarFoodsModel();
            carFoodsModel.setZl(zweekmenubak.getZl());
            carFoodsModel.setAddr(zweekmenubak.getAddr());
            carFoodsModel.setBm(zweekmenubak.getBm());
            carFoodsModel.setName(zweekmenubak.getName());
            carFoodsModel.setPrice(zweekmenubak.getPrice());
            carFoodsModel.setFoodNumber(1);
            list.add(carFoodsModel);
        }
        amountCar();
        notifyDataSetChanged();
    }


    public void addFoods(ProductModel.Device productModel) {
        CarFoodsModel carFoodsModel = list.stream().filter(u -> u.getBm().equals(productModel.getDeviceID()))
                .findFirst()
                .orElse(null);
        if (carFoodsModel != null) {
            if (new BigDecimal(carFoodsModel.getFoodNumber()).compareTo(productModel.getNumber()) < 0) {
                carFoodsModel.setFoodNumber(carFoodsModel.getFoodNumber() + 1);
            } else {
                carFoodsModel.setFoodNumber(productModel.getNumber().intValue());
            }

        } else {
            carFoodsModel = new CarFoodsModel();
            carFoodsModel.setZl("9");
            carFoodsModel.setAddr(1);
            carFoodsModel.setBm(productModel.getDeviceID());
            carFoodsModel.setName(productModel.getDevicename());
            carFoodsModel.setPrice(productModel.getSale_price());
            carFoodsModel.setPriceVer(productModel.getPriceVer());
            carFoodsModel.setFoodNumber(1);
            carFoodsModel.setPid(productModel.getPid());
            carFoodsModel.setPriceAdjustTimes(productModel.getPriceAdjustTimes());
            carFoodsModel.setProvider(productModel.getProvider());
            carFoodsModel.setMaxNumber(productModel.getNumber().toPlainString());
            list.add(carFoodsModel);
        }
        amountCar();
        notifyDataSetChanged();
    }

    public List<CarFoodsModel> getList() {
        return list;
    }

    public void clearCar() {
        list = new ArrayList<>();
        amountCar();
        notifyDataSetChanged();

    }

    static class ViewHolder extends RecyclerView.ViewHolder {
        TextView foodNameTv;
        AddAndDecreaseButton foodNumberTv;
        TextView priceTv;
        Button delFoodBut;


        public ViewHolder(View view) {
            super(view);
            foodNameTv = (TextView) view.findViewById(R.id.foodNameTv);
            foodNumberTv = view.findViewById(R.id.foodNumberTv);
            delFoodBut = view.findViewById(R.id.delFoodBut);
        }
    }


    @NonNull
    @Override
    public ViewHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
        View view = LayoutInflater.from(parent.getContext()).inflate(R.layout.item_select_foods, parent, false);
        ViewHolder holder = new ViewHolder(view);
        return holder;
    }


    @Override
    public void onBindViewHolder(@NonNull ViewHolder holder, int position) {
        holder.foodNameTv.setText(list.get(position).getName());
        holder.foodNumberTv.setText(list.get(position).getFoodNumber(), list.get(position).getMaxNumber());

        holder.delFoodBut.setOnClickListener(v -> {
            //删除按钮点击
            list.remove(position);
            amountCar();
            notifyDataSetChanged();
        });

        holder.foodNumberTv.setAddAndDecreOnClick((type, amount) -> {
            list.get(position).setFoodNumber(amount);
            amountCar();
        });
    }

    @Override
    public int getItemCount() {
        return list.size();
    }

    private void amountCar() {
        BigDecimal amountPrice = new BigDecimal(0);
        BigDecimal amountCount = new BigDecimal(0);
        for (CarFoodsModel goo : list) {
            amountCount = amountCount.add(new BigDecimal(goo.getFoodNumber()));
            amountPrice = amountPrice.add((new BigDecimal(goo.getFoodNumber()).multiply(new BigDecimal(goo.getPrice()))));
        }
        if (carFoodsAmount != null) {
            carFoodsAmount.amountCar(amountPrice, amountCount);
        }
    }

    public interface CarFoodsAmountListener {
        public void amountCar(BigDecimal amountPrice, BigDecimal amountCount);
    }

}
